package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: CheckItemServiceImpl
 * @Author: XLB
 * @Date: 2020/2/29 15:00
 * @Description:检查项服务
 */

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    //注入DAO对象
    @Autowired
    private CheckItemDao checkItemDao;

    //检查项增加
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }


    //检查项分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        //完成分页查询，基于mybatis框架提供的分页助手插件完成
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //根据查询条件查询  底层已经封装好分页查询的sql语句
        // select * from t_checkitem limit 0,10
        Page<CheckItem> page = checkItemDao.selectByCondition(queryPageBean.getQueryString());

        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();

        return new PageResult(total,rows);
    }

    //删除检查项
    public void delete(Integer id) {
        //判断当前检查项是否已经关联到检查组，因为多表之间关系，需要判断能不能被删除
        Long count=checkItemDao.findCountByCheckItemId(id);
        if (count>0){
            //当前检查项已经被关联到检查组
            new RuntimeException();
        }
        //没被关联
        checkItemDao.delete(id);
    }

    //编辑检查项回显
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    //编辑检查项
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    //检查组新增：检查项全部查询
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }


}
